﻿/*
department : 360 beacon
write by   : wuweiwei
github     : www.github.com/flybirdsoft
date       : 2015/10/28
*/

(function(win){
	var wMobileDialog = {
		version : "1.0",
		body    : null
	};

	wMobileDialog.init = function(options){
		/*
		options={
			maskColor:"#ccc", 可选参数
			opacity  : .5,    可选参数
			zIndex   : 2000,  可选参数
			className: "",    可选参数
			width:   : "100px", 可选参数
			height   : "200px", 可选参数
			isClickClose : false|true,默认为flase 可选参数
		}
		*/
		this.body = document.getElementsByTagName("body")[0];
		this.options = options;
		if(this.options==undefined)
		{
			this.options = {};
		}
		this.create();
		this.bindEvent();
	};

	wMobileDialog.create = function(){
		var mask = document.createElement("div");
		mask.id = "wMobileTip_mask";
		mask.style.position = "fixed";
		mask.style.top = "0";
		mask.style.left = "0";
		mask.style.width = "100%";
		mask.style.height = "100%";
		mask.style.backgroundColor = this.options.maskColor || "#000";
		mask.style.opacity = this.options.opacity || 0.5;
		mask.style.display = "none";

		mask.style.zIndex = this.options.zIndex || 2000;
		this.body.appendChild(mask);
		this.mask = mask;

		var dialog = document.createElement("div");
		this.dialog = dialog;
		dialog.id = "wMobileTip_dialog";
		dialog.style.position = "fixed";
		dialog.style.top = "50%";
		dialog.style.left = "50%";
		/*
		dialog.style.marginLeft = "-25%";
		dialog.style.marginTop = "-25%";
		*/
		dialog.style.overflow = "hidden";
		
		if(this.options.width!=undefined)
		{
			dialog.style.width = this.options.width + "px";
		}
		if(this.options.height!=undefined)
		{
			dialog.style.height = this.options.height + "px";
		}
		dialog.style.display = "none";
		dialog.style.zIndex = this.options.zIndex == undefined ? 2010 : this.options.zIndex+10;

		if(this.options.className != undefined)
		{
			this.dialog.className = this.options.className;
		}
		this.body.appendChild(dialog);
	}

	wMobileDialog.bindEvent = function(){
		var th = wMobileDialog;
		if(this.options.isClickClose!=undefined)
		{
			this.mask.onclick = this.dialog.onclick = function(e){
				th.hide();
			}
		}
	}

	wMobileDialog.show = function(options){
		/*
		optioins = {
			text : "",
			container:dom,
			timeout : n,
			mask : true|false,默认为false
		}
		*/
		var th = this;
		var offsetWidth=0  , offsetHeight=0;
		if(options==undefined)
		{
			options = {};
		}
		this.dialog.innerHTML = options.text || "";
		if(options.container!=undefined)
		{
			this.dialog.appendChild(options.container);
			this.container = options.container;
			this.container.style.display = "block";
		}

		if(options.mask!=undefined && options.mask)
		{
			this.mask.style.display = "block";
		}
		this.dialog.style.display = "block";
		if(options.timeout!=undefined)
		{
			setTimeout(function(){
				th.dialog.style.display = "none";
			},options.timeout);
		}
		offsetWidth = this.dialog.offsetWidth;
		offsetHeight = this.dialog.offsetHeight;
		this.dialog.style.marginLeft = "-" + offsetWidth/2 + "px";
		this.dialog.style.marginTop = "-" + offsetHeight/2 + "px";
	}

	wMobileDialog.hide = function(){
		this.dialog.style.display = "none";
		this.mask.style.display = "none";
		if(this.container!=undefined)
		{
			this.container.style.display = "none";
			this.body.appendChild(this.container);
		}
	}

	win["wMobileDialog"] = wMobileDialog;
})(window);


/*

调用示例：
wMobileDialog.init()函数的参数都可选

wMobileDialog.init({
	className:"tip",
	isClickClose:true,
	opacity:.2,
	maskColor:"#F00",
});
或
wMobileDialog.init();


1.显示自定义对话框：
$("#btn").click(function(){
	wMobileDialog.show({
		container:$("#dialog")[0],
		mask:true
	});
});

2.显示提示框：
$("#btn").click(function(){
	wMobileDialog.show({
		text : "提示信息",
		timeout:1000
	});
});


隐藏对话框：
$("#btn2").click(function(){
	wMobileDialog.hide();
});


*/

